﻿(function ($) {

    $(function () {
        var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
        $.getScript(scriptbase + "SP.Runtime.js", function () {
            $.getScript(scriptbase + "SP.js", function () {
                $.getScript(scriptbase + "SP.Taxonomy.js", GetCategories('category'));
            });
        }
        );
    });

    /*javascript for News Category*/
    this.GetCategories = function (idcontent) {
        $('#leftmenuloading').show();
        var siteUrl = '/teams/OurVoiceHN/News/';
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Categories');
        var items = oList.getItems(new SP.CamlQuery());
        clientContext.load(items, "Include(ID,Title)");
        clientContext.executeQueryAsync(
            function () {
                $('#' + idcontent).html('');
                $('#leftmenuloading').hide();
                var listEnumerator = items.getEnumerator();
                var result = "";                
                while (listEnumerator.moveNext()) {
                    var item = listEnumerator.get_current();
                    var url = siteUrl + 'Lists/Categories/Category.aspx?CategoryId=' + item.get_item('ID');
                    var category = BuildCategory(item.get_item('Title'), url);
                    result += category;
                }
                if (result == "") {
                    result = "There are no items in this category.";
                }
                $('#' + idcontent).append('<div class="title">Categories</div> <ul class="static">' + result + '</ul>');

            }, function (sender, args) {

                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            });
    }
    this.BuildCategory = function (title, url) {
        if (title == null)
        { title = ""; }
        var category = '<li class="static"><a href="' + url + '" class="ms-core-listMenu-item ms-blog-quickLinksEntry"><nobr>' + title + '</nobr></a></li>';
        return category;
    };
    /*end javascript for News Category*/
    /*=======================================*/

}).call(this, jQuery);